package org.nanquan.dotjava.tags;

import java.lang.reflect.InvocationTargetException;

import javax.servlet.jsp.PageContext;

public class RunMethod{
	
	public  Class<?> []  getMethodType(Object ... args) {
		if(args==null)
		{
			return null;
		}
		Class<?> [] classes=new Class[args.length];
		for (int i = 0; i < args.length; i++) {
			classes[i]=args[i].getClass();
		}
		
		return classes;
	}
	
	public Object execute(Object obj,String methodname,Object ... args) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
				
		return obj.getClass().getMethod(methodname,getMethodType(args)).invoke(obj, args);
				
	}
	
	public Object execute(Object obj,String methodname,Object [] args,Class<?>[] types) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		
		return obj.getClass().getMethod(methodname,types).invoke(obj, args);
				
	}
	
//	public Object getObj(PageContext pageContext,String fullname)
//	{
//		String [] names=fullname.split(".");
//		int index=fullname.indexOf(".");
//		if(index>=0)
//		{
//			String name1=fullname.substring(0,index);
//			String name2=fullname.substring(index);
//			try {
//				TagUtils.getProperty(TagUtils.findAttribute(name1, pageContext), name2);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		else
//		{
//			
//		}
//		
//		
//	}
	
	public Object getObjFromScopeByName(PageContext pageContext,String name)
	{
		Object obj=null;
		if(pageContext.getAttribute(name)!=null)
		{
			obj=pageContext.getAttribute(name);
		}
		else if(pageContext.getRequest().getAttribute(name)!=null)
		{
			obj=pageContext.getRequest().getAttribute(name);
		}
		else if(pageContext.getSession().getAttribute(name)!=null)
		{
			obj=pageContext.getSession().getAttribute(name);
		}
		else if(pageContext.getServletContext().getAttribute(name)!=null)
		{
			obj=pageContext.getServletContext().getAttribute(name);
		}
		return obj;
	}
}
